<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="收货人：">
        <span>联系人：{{orderDetail.receiverName}}/{{orderDetail.receiverMobile}}</span><br />
        <span>地址：{{orderDetail.receiverProvince}}{{orderDetail.receiverCity}}{{orderDetail.receiverRegion}} {{orderDetail.receiverAddress}}</span>
      </el-form-item>
      <el-form-item label="包裹号：" prop="packageNo" v-if="type=='edit'">
        <el-select size="small" v-model="ruleForm.packageNo" placeholder="请选择">
          <el-option
                  v-for="item in packages"
                  :key="item.packageNo"
                  :label="item.packageNo"
                  :value="item.packageNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="快递公司：" prop="expressCompany">
        <el-select size="small" v-model="ruleForm.expressCompany" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.expressCompany"
            :label="item.expressCompany"
            :value="item.expressCompany">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号：" prop="expressNo" v-if="ruleForm.expressCompany !='海外圆通A' && ruleForm.expressCompany !='海外圆通B'">
        <el-input size="small" v-model="ruleForm.expressNo"></el-input>
      </el-form-item>
      <el-form-item label="发货类型：" v-if="type === 'confirm'" prop="deliverType">
        <el-radio-group v-model="ruleForm.deliverType" @change="deliverTypeChange">
          <el-radio label="0">按订单发货</el-radio>
          <el-radio label="1">商品分包裹发货</el-radio>
        </el-radio-group>
        <br />
        <span>选择商品发货类型</span>
      </el-form-item>
      <el-form-item v-if="ruleForm.deliverType === '1' && type === 'confirm'" class="goods" label="发货商品：">
        <el-checkbox-group v-model="arr" @change="handleChecked">
          <div v-if="orderDetail.goodDetailVoList">
            <el-checkbox :label="item" name="type" v-for="(item, index) in orderDetail.goodDetailVoList" :key="index">
              <el-input size="mini" v-model="item.goodsNum" placeholder="数量"></el-input>
              <img :src="item.picture">
              <span>{{item.goodsName}}</span>
            </el-checkbox>
          </div>
          <div v-else>
            <el-checkbox :label="item" name="type" v-for="(item, index) in orderDetail.goodVoList" :key="index">
              <el-input size="mini" v-model="item.goodsNum" placeholder="数量"></el-input>
              <img :src="item.picture">
              <span>{{item.goodsName}}</span>
            </el-checkbox>
          </div>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" @click="submit">确认发货</el-button>
      <el-button size="small" @click="close(false)">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import api from "@/api/order.service.js";

  export default {
    name: 'receiverInfo',
    data() {
      return {
        ruleForm: {
          deliverType: '0'
        },
        rules: {
          expressNo: [
            { required: true, message: '请输入快递单号', trigger: 'blur'}
          ],
          expressCompany: [
            { required: true, message: '请选择快递公司', trigger: 'change'}
          ],
          packageNo: [
            { required: true, message: '请选择包裹号', trigger: 'change'}
          ]
        },
        options: [], // 快递公司数据
        packages: [], // 包裹号数据
        arr: []
      }
    },
    props: {
      orderDetail: {
        type: Object,
        default: () => {
          return {}
        }
      },
      type: {
        type: String,
        default: () => {
          return 'confirm'
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() { // 查询快递公司数据
        api.companyList({pageNo: 1, pageSize: 100}).then(res => {
          this.options = res.values
        })
        api.getPackageNo({orderId: this.orderDetail.id}).then(res => {
          this.packages = res.value
        })
      },
      close(flag) {
        this.$emit('close', flag)
      },
      deliverTypeChange() {
        this.ruleForm.goods = null
        this.arr = []
      },
      handleChecked(val) {
        let goods = []
        val.map(res => {
          goods.push({orderItemId: res.id, goodsNum: res.goodsNum})
        })
        this.ruleForm.goods = goods
      },
      submit() {
        let url = null
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.ruleForm.orderNo = this.orderDetail.orderNo
            this.type == 'confirm' ? url = 'orderDeliver' : url = 'updateDeliverInfo'
            api[url](this.ruleForm).then(res => {
              if (res.code == 200) {
                this.close(true)
                this.$message({
                  message: this.$t('common.operateSucccess'),
                  type: 'success'
                })
              }
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .dialog-footer {
    text-align: center;
  }
  .goods {
    img {
      width: 25px;
      height: 25px;
      vertical-align: bottom;
      margin-right:10px;
    }
    .el-checkbox-group {
      display: grid;
    }
    .el-input {
      width: 60px;
    }
  }
</style>
